package com.plamwind;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Vector;

import com.palmwindActivity.proto.ProtoBuf;
import com.palmwindActivity.proto.ProtoBufUtil;
import com.palmwindActivity.proto.TextUpdate;


public class TextUpdateService {
	
	public static boolean serve(ProtoBuf[] protoBuf, OutputStream ouputStream) throws IOException {
			boolean isSuccess = false;
			if (protoBuf != null) {
				char[] data = readFile(IServiceConstants.COMMENT_FILE_TEXT);
				if(data != null)
				{
					Vector<ProtoBuf>  vector = createProtoBufResp(data);
					byte[] readData = ProtoBufUtil.listToByteArray(vector);
					ouputStream.write(readData);
					ouputStream.flush();
					isSuccess = true;
				}
			}
			return isSuccess;
		}
	
	protected static Vector<ProtoBuf> createProtoBufResp(char[] data) {
		ProtoBuf protoBuf = new ProtoBuf();
		TextUpdate.TextUpdateResp.Builder textUpdateRespBuilder = TextUpdate.TextUpdateResp
				.newBuilder();
		
		String dataString = new String(data);
		String lines[] = dataString.split("&&&&");
		boolean isSuccessful = false;
		if (lines != null) {
			for (int i = 0; i < lines.length; i++) {
				TextUpdate.TextUpdateResp.TextComment.Builder textCommentBuilder = TextUpdate.TextUpdateResp.TextComment
				.newBuilder();
				String[] commentLines = lines[i].split("::");
				if(commentLines != null && commentLines.length == 3)
				{
					textCommentBuilder.setUsername(commentLines[0]);
					String timeStampStr = commentLines[1];
					long commentTimeStamp =  Long.parseLong(timeStampStr);
					textCommentBuilder.setTimeStamp(commentTimeStamp);
					textCommentBuilder.setComments(commentLines[2]);
					textUpdateRespBuilder.addComments(textCommentBuilder.build());
					isSuccessful = true;
				}
				
			}
		}
		if(isSuccessful)
		{
			textUpdateRespBuilder.setResponse(0);
		}
		else
		{
			textUpdateRespBuilder.setResponse(1);
		}
		textUpdateRespBuilder.setAction(IServiceConstants.ACTION_GET_TEXT);
		TextUpdate.TextUpdateResp resp = textUpdateRespBuilder.build();
		 protoBuf.setBufferData(resp.toByteArray());
		 
		 protoBuf.setObjType(IServiceConstants.ACTION_GET_TEXT);
		 Vector<ProtoBuf> vector = new Vector<ProtoBuf>();
		 vector.add(protoBuf);
		return vector;
	}

	protected static char[] readFile(String fileName) {
		File file = new File(fileName);
		FileReader fileReader = null;
		FileInputStream fis = null;
		char[] buf = null;
		 InputStreamReader bis = null;
		 BufferedReader bufreader = null;

		try {
		
			fis = new FileInputStream(file);
//			fileReader = new FileReader(file);
//			BufferedReader bufReader = new BufferedReader(fileReader);
			// Here BufferedInputStream is added for fast reading.
			 bis = new InputStreamReader(fis);
			 bufreader = new BufferedReader(bis);

			// dis.available() returns 0 if the file does not have more lines.
		
			int size = fis.available();
			buf = new char[size];
			bufreader.read(buf);
			// dispose all the resources after using them.

			// 

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fis.close();
				bis.close();
				bufreader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return buf;
	}

}
